// Fill out your copyright notice in the Description page of Project Settings.


#include "Player/AuraHUD.h"

#include "UI/Widget/AuraUserWidget.h"
#include "UI/WidgetController/AttributeMenuWidgetController.h"
#include "UI/WidgetController/AuraWidgetController.h"
#include "UI/WidgetController/OverlayWidgetController.h"


UOverlayWidgetController* AAuraHUD::GetOverlayWidgetController(const FWidgetControllerProps& WCProps)
{
	if (OverlayWidgetController == nullptr)
	{
		UOverlayWidgetController* WidgetController = NewObject<UOverlayWidgetController>(
			this, OverlayWidgetControllerBPClass);
		WidgetController->SetWidgetControllerProps(WCProps);
		
		return WidgetController;
		
	}
	return OverlayWidgetController;
}

UAttributeMenuWidgetController* AAuraHUD::GetAttributeMenuWidgetController(const FWidgetControllerProps& WCProps)
{
	if (AttributeMenuWidgetController == nullptr)
	{
		AttributeMenuWidgetController = NewObject<UAttributeMenuWidgetController>(this,AttributeMenuWidgetControllerClass);
		AttributeMenuWidgetController->SetWidgetControllerProps(WCProps);
		AttributeMenuWidgetController->BindCallbacksToDependencies();
		return AttributeMenuWidgetController;
	}
	return AttributeMenuWidgetController;
}

void AAuraHUD::InitOverlay(APlayerController* PC, APlayerState* PS, UAbilitySystemComponent* ASC, UAttributeSet* AS)
{
	checkf(OverlayWidgetClass,TEXT("OverlayWidgetClass 未初始化,请在HUD中设置"));					
	checkf(OverlayWidgetControllerBPClass,TEXT("OverlayWidgetControllerClass 未初始化,请在HUD中设置"));
	
	//创建OverlayWidget控件
	OverlayWidget = Cast<UAuraUserWidget>(CreateWidget(GetWorld(),OverlayWidgetClass));
	const FWidgetControllerProps WidgetControllerProps(PC,PS,ASC,AS);
	// 初始化OverlayWidget控制器
	UOverlayWidgetController* OverlayWC = GetOverlayWidgetController(WidgetControllerProps);//创建控件控制器
	OverlayWidgetController = OverlayWC;
	
	//设置OverlayWidget控件的控制器
	OverlayWidget->SetWidgetController(OverlayWC );

	// 绑定控制器的相关回调
	OverlayWC->BindCallbacksToDependencies();

	
	//广播初始值函数,调用委托广播
	OverlayWidgetController->BroadcastInitialValues();

	OverlayWidget->AddToViewport();//控件添加到视口

}

void AAuraHUD::BeginPlay()
{
	Super::BeginPlay();


}
